package net.csdn.business.webhook.config;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.google.common.collect.Maps;
import lombok.Data;
import net.csdn.business.webhook.listener.WebhookMessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.Map;
import java.util.Optional;
import java.util.Properties;

/**
 * @ClassName MqConfig
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/5/5 18:44
 */
@Data
@Configuration
@RefreshScope
public class MqConfig {
    private static final String DEFAULT_MAX_TIMES = "5";
    private static final String DEFAULT_THREAD_NUMS = "5";

    @Value("${ali.mq.accessKey:LTAI5tKPzhpwVq1VssuujnL8}")
    private String accessKey;

    @Value("${ali.mq.secretKey:sYmmWUOvOo4xWnv6Wt4NvmVdor7CkS}")
    private String secretKey;

    @Value("${ali.mq.tcp.nameSrvAddr:http://MQ_INST_1758307656022564_BYVB9xpQ.cn-hangzhou.mq.aliyuncs.com:80}")
    private String tcpNameSrvAddr;

    @Value("${ali.mq.webhook.groupId:GID_WEB_HOOK_EVENT_CONSUMER}")
    private String webhookGroupId;

    @Value("${ali.mq.instanceId:MQ_INST_1510946222818309_BYDdVx1H}")
    private String instanceId;

    @Value("${ali.mq.webhook.topic:WEB_HOOK_EVENT}")
    private String webhookTopic;

    @Value("${ali.mq.consumer.max:5}")
    private String consumerMaxTimes;
    @Value("${ali.mq.consumer.thread.num:5}")
    private String consumerThreadNums;


    @DependsOn("webhookMessageListener")
    @Bean(name = "webhookEventConsumer", initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean initAliProjectChangeConsumer(WebhookMessageListener webhookMessageListener) {
        ConsumerBean webhookEventConsumer = new ConsumerBean();
        Properties properties=getMqProperties();
        properties.setProperty(PropertyKeyConst.GROUP_ID, webhookGroupId);
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, Optional.ofNullable(consumerMaxTimes).orElse(DEFAULT_MAX_TIMES));
        // 默认 20-64
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, Optional.ofNullable(consumerThreadNums).orElse(DEFAULT_THREAD_NUMS));
        webhookEventConsumer.setProperties(properties);
        //订阅关系
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(webhookTopic);
        //subscription.setExpression(projectTag);
        subscriptionTable.put(subscription, webhookMessageListener);
        webhookEventConsumer.setSubscriptionTable(subscriptionTable);
        return webhookEventConsumer;
    }


    @Bean(name = "aliProducer", initMethod = "start", destroyMethod = "shutdown")
    public ProducerBean buildReportProducer() {
        ProducerBean producer = new ProducerBean();
        Properties properties = getMqProperties();
        producer.setProperties(properties);
        return producer;
    }

    private Properties getMqProperties() {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, this.accessKey);
        properties.setProperty(PropertyKeyConst.SecretKey, this.secretKey);
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, this.tcpNameSrvAddr);
        return properties;
    }

}